<?php
defined('_JEXEC') or die('Restricted access');
global $mainframe;
$document =& JFactory::getDocument();
$user	= & JFactory::getUser();
$uid    =	$user->id;
$document->addStyleSheet('components/com_wrecruit/css/other_employers.css');
$document->addScript ('components/com_wrecruit/js/other_employers.js');
$document->addScript( 'components/com_wrecruit/js/emppopup.js' );
$document->addStyleSheet('components/com_wrecruit/css/emppopup.css');
$model = $this->getModel('wrecruit', 'WRecruitModel');
//$document->addScript ('components/com_wrecruit/js/jquery-1.7.2.min.js');
  //$document->addScript ('components/com_wrecruit/js/jquery-ui.min.js');
  $document->addScript ('components/com_wrecruit/js/employer_autocomplete.js');
 $candidate = $this->getModel('wrecruit', 'WRecruitModel');
  $user	=& JFactory::getUser();
  if(isset($user->id) && $user->id == 0 || $user->id == ''){
	   $app =& JFactory::getApplication(); 
	   $app->enqueueMessage( JText::_( 'Please <a href="index.php?option=com_users&view=login">Login</a>' ), 'Login access' );
	  return;
  }else{
  	$candidateObj = $candidate->getAllCandidatesInfor($user->id);
	if( !$candidateObj ){
		$app =& JFactory::getApplication(); 
       //add a message to the message queue
        $app->enqueueMessage( JText::_( 'Please <a href="index.php?option=com_users&view=login">Login</a>' ), 'Login access' );

		return;
	}
	
   }
   
?>
<style type="text/css">

</style>
<script language="Javascript">
function markMessage(chk,rd){
	var checkbox = document.getElementById('frm_emp').elements[chk];
	var items ="";
	for(var i=0;i<checkbox.length;i++) {
		if(checkbox[i].checked==true){
			items = items + checkbox[i].value+",";
		}
	}
	if(items=="" || items==null){
		alert("Pls check at least one item to mark!");
	}else {
		window.location = "index.php?option=com_wrecruit&c=wrecruit&task=markmessage&box=<?php echo JRequest::getVar('box');?>&page=<?php echo JRequest::getVar('page');?>&sort=<?php echo JRequest::getVar('sort');?>&Itemid=<?php echo $this->Itemid; ?>&isread="+rd+"&elements="+items;
	}	
	
}
//DELETE message
function removeMessage(chk){
	var checkbox = document.getElementById('frm_emp').elements[chk];
	var items ="";
	for(var i=0;i<checkbox.length;i++) {
		if(checkbox[i].checked==true){
			items = items + checkbox[i].value+",";
		}
	}
	if(items=="" || items==null){
		alert("Pls check at least one item to delete!");
	}else {
		var answer = confirm("Are you sure to delete?")
		if (answer){
			window.location = "/index.php?option=com_wrecruit&c=wrecruit&task=removemessage&box=<?php echo JRequest::getVar('box');?>&sort=<?php echo JRequest::getVar('sort');?>&page=<?php echo JRequest::getVar('page');?>&Itemid=<?php echo $this->Itemid; ?>&elements="+items;
		}
		else{
			//alert("Cancelled!")
		}
	}	
}
function getFieldtoSort(val) {
	window.location="/index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=<?php echo JRequest::getVar('box');?>&page=<?php echo JRequest::getVar('page');?>&Itemid=<?php echo JRequest::getVar('Itemid');?>&sort="+val;
}
function confirmDelete() {
	return confirm("Are you sure to delete?");
}
</script>

<?php if ($this->config['offline'] == '1'){ ?>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
	<tr>
		<td valign="top" class="<?php echo $this->theme['title']; ?>"><?php echo $this->config['title']; ?>
		</td>
	</tr>
	<tr>
		<td height="25"></td>
	</tr>
	<tr>
		<td class="wrecruitmsg"><?php echo $this->config['offline_text']; ?>
		</td>
	</tr>
</table>
<?php }
else{
	if (isset($this->userrole->rolefor)){
		if ($this->userrole->rolefor == 1) // employer
		$allowed = true;
		else
		$allowed = false;
	}
	else { if ($this->config['visitorview_emp_conrolpanel'] == 1) $allowed = true; else $allowed = false; } // user not logined
	if ($allowed == false) {
		?>
<div class="box-list-job">
	<div class="job-seeker-panel">
	<?php include 'menuLeft.php';?>
	</div>
	<div class="job-seeker-right">
		<div class="head-title">
			<h2>Message Listing</h2>
		</div>
		<form name="frm_msg" id="frm_emp" method="post"	action="index.php">
			<div class="message-list">
				<div class="box">
					<a href="<?php echo JURI::base()."index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=candidate_sent&sort=DESC&page=1&Itemid=". $this->Itemid; ?>">
						<span>Inbox</span>
					</a>
					<a href="<?php echo JURI::base()."index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=employer_sent&sort=DESC&page=1&Itemid=". $this->Itemid; ?>">
						<span>Sent</span>
					</a>
				</div>
				<div class="menu">
					<span style="float:right" onclick="removeMessage('checked[]')">Delete</span>
					<span onclick="markMessage('checked[]',1)">Mark read</span>
					<span onclick="markMessage('checked[]',0)">Mark unread</span>
				</div>
				<div class="sort"> 
					<input type="checkbox" name="all" id="all" onclick="checkedAll()" />
					<div style="float:right">
						<select class="dropdown-box" id="salaryrangefrom" name="sort" onchange="getFieldtoSort(this.value)">
							<option value="DESC" selected="selected">Newest</option>>
							<option value="ASC">Oldest</option>>
						</select>
						
					</div>
				</div>
				<?php  foreach ($this->messages as $item) {?>
				<div class="item">
					<div class="sub-item">
						<span style="float:right"><?php echo date('M d',strtotime($item->created)); ?></span>
						<div>
							<div class="pic">
								<span style="padding-right:5px;vertical-align: top;">
									<input type="checkbox" name="checked[]" value="<?php echo $item->id ?>" />
								</span>
								<img alt="photo" src="components/com_wrecruit/images/candidate_pic.png" />
							</div>
							<div>
								<span><?php echo $item->name ?></span><br/>
								<a style="color:#000;" href="<?php JURI::base();?>index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagedetail&mid=<?php echo $item->id;?>&Itemid=<?php echo $this->Itemid;?>">
									<?php if($item->isread==0 && JRequest::getVar('box')=='candidate_sent') {?>
										<?php echo '<strong>'.$item->subject.'</strong>'; 
											}else {
												echo $item->subject;
										}?>
								</a>
								<div>
									<a class="reply" href="#">Reply</a>|
									<a class="forward" href="#">Forward</a>|
									<a onclick="return confirmDelete()" href="/index.php?option=com_wrecruit&c=wrecruit&task=removeOneMessage&box=candidate_sent&sort=<?php echo JRequest::getVar('sort');?>&page=<?php echo JRequest::getVar('page');?>&Itemid=<?php echo $this->Itemid; ?>&mid=<?php echo $item->id;?>">Delete</a></div>
							</div>
						</div>
					</div>
				</div>
				<input type="hidden" id="cname" name="<?php echo $item->name;?>" />
				<input type="hidden" id="email" name="<?php echo $item->email;?>" /> 
				<input type="hidden" id="cid" name="<?php echo $item->jobseekerid;?>" />
				<input type="hidden" id="empid" name="<?php echo $item->employerid;?>" />
				<input type="hidden" id="subject" name="<?php echo $item->subject;?>" />
				<?php }?>
				<div class="menu">
					<span style="float:right" onclick="removeMessage('checked[]')">Delete</span>
					<span onclick="markMessage('checked[]',1)">Mark read</span>
					<span onclick="markMessage('checked[]',0)">Mark unread</span>
				</div>
			</div>	
		</form>
		<div class="pagination" style="clear:both;">
			<?php
                 $db = &JFactory::getDBO();			
				$box = JRequest::getVar("box");
				$sql = "SELECT MSG.id FROM ".$db->nameQuote('#__wrecruit_messages')." AS MSG 
						LEFT JOIN ".$db->nameQuote('#__wrecruit_candidates')." AS CAND ON MSG.jobseekerid=CAND.id
						INNER JOIN ".$db->nameQuote('#__users')." AS  USR ON CAND.uid= USR.id 
						INNER JOIN ".$db->nameQuote('#__wrecruit_employers')." AS  EMP ON EMP.id= MSG.employerid
						WHERE ".$box."=1 AND EMP.uid = ".$uid;
				$page = JRequest::getVar("page");
				$number_show = 5;
				if($page > 1){
					$p	=	$page;
				}
				else{
					$p	=	1;
				}
				$start	=	($p-1)*$number_show;
				$total  = $candidate->getTotalRows($sql);
				print_r($total);
				if($p!=1){?>
		    	<a href="index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=<?php echo JRequest::getVar('box');?>&sort=<?php echo JRequest::getVar('sort');?>&Itemid=<?php echo JRequest::getVar('Itemid');?>&page=<?php echo $p-1;?>">Previous</a>
		    	<?php }
					for($i=0;$i<$total;$i=$i+$number_show){
						$count++;?>
				   		 <a href="index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=<?php echo JRequest::getVar('box');?>&sort=<?php echo JRequest::getVar('sort');?>&Itemid=<?php echo JRequest::getVar('Itemid');?>&page=<?php echo ($i/$number_show)+1;?>"><? echo ($i/$number_show)+1;?></a><?php 
					}
					if($p!=$count){?>
				   		<a href="index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=messagelist&box=<?php echo JRequest::getVar('box');?>&sort=<?php echo JRequest::getVar('sort');?>&Itemid=<?php echo JRequest::getVar('Itemid');?>&page=<?php echo $p+1;?>">Next</a>
				<?php
					}
				?>
		</div>
	</div>	
</div>
<?php } else{ // not allowed job posting
		//Update by Vireak on 28/06/2012
		//Add frame to this warning and some styling
		//---------------------
		echo '<div class="box-list"><div class="head-title"><h2>Oop!</h2></div>';
		echo '<span class="wr_info"><p>'.JText::_('JS_YOU_ARE_NOT_ALLOWED_TO_VIEW').'</p>';
		echo '<p>This page is reserved for the authorized employers or recruiters only. </p></span>';
		echo '</div>';
		//---------------------
	}
}//ol
?>
<div id="popupContainer" class="hidden" style="text-align:left;">
     fdfddfd
</div>
<div id="overlayEffect">
</div>
<div width="100%">
<?php
if($this->config['fr_cr_txsh']) {
	echo
	'<table width="100%" style="table-layout:fixed;">
		<tr><td height="15"></td></tr>
		<tr><td style="vertical-align:top;" align="center">'.$this->config['fr_cr_txa'].$this->config['fr_cr_txb'].'</td></tr>
	</table>';
}
?>
</div>
